package com.wk.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wk.common.bean.PageVo;
import com.wk.common.bean.Query;
import com.wk.common.bean.QueryCondition;
import com.wk.common.bean.Resp;
import com.wk.pms.entity.SpuInfo;
import com.wk.pms.service.SpuInfoService;
import com.wk.pms.vo.SpuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spuinfo")
public class SpuInfoController {

    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 分页查询
     */
    @PostMapping("page")
    public Resp<List<SpuInfo>> page(@RequestBody QueryCondition queryCondition) {
        List<SpuInfo> list = spuInfoService.page(new Query<SpuInfo>().getPage(queryCondition)).getRecords();
        return Resp.ok(list);
    }
    
    /**
     * 商品列表,根据分类id或关键字查询数据并分页
     */
    @GetMapping
    public Resp<PageVo> querySpuPage(QueryCondition queryCondition,
                                     @RequestParam("catId") Long categoryId){
        PageVo page = spuInfoService.querySpuPage(queryCondition,categoryId);

        return Resp.ok(page);
    }

    /**
     * 新增spu、sku以及营销信息
     */
    @PostMapping("/save")
    public Resp<Object> save(@RequestBody SpuInfoVO spuInfoVo){
        spuInfoService.saveSpuInfo(spuInfoVo);

        return Resp.ok(null);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public Resp<PageVo> list(@RequestBody QueryCondition queryCondition) {
        IPage<SpuInfo> page = spuInfoService.page(
                new Query<SpuInfo>().getPage(queryCondition),
                null
        );

        return Resp.ok(new PageVo(page));
    }

    @GetMapping("/info/{id}")
    public Resp<SpuInfo> info(@PathVariable("id") Long id){
        SpuInfo spuInfo = spuInfoService.getById(id);
        return Resp.ok(spuInfo);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Resp<Object> update(@RequestBody SpuInfo spuInfo){
        spuInfoService.updateOne(spuInfo);

        return Resp.ok(null);
    }
}

